package com.mjlf.test_shiro.shiro;

import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @ClassName: ShiroConfig
 * @Author: majun
 * @CreateDate: 2018/10/17 16:22
 * @Version: 1.0
 * @Description: TODO
 */

@Configuration
public class ShiroConfig {

    @Bean
    public UserRealm userRealm() {
        return new UserRealm();
    }

    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(UserRealm userRealm) {
        DefaultWebSecurityManager  securityManager = new DefaultWebSecurityManager ();
        securityManager.setRealm(userRealm);
        return securityManager;
    }

    @Bean(name = "lifecycleBeanPostProcessor")
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean(name = "shiroFilter")
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        return shiroFilterFactoryBean;
    }

//    /**
//     * 凭证匹配器
//     * @return
//     */
//    @Bean
//    public HashedCredentialsMatcher hashedCredentialsMatcher(){
//        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
//
//        //对凭证进行加密, 下边是使用md5的加密方式加密一次
////        hashedCredentialsMatcher.setHashAlgorithmName("md5");
////        hashedCredentialsMatcher.setHashIterations(1);
//
//        return hashedCredentialsMatcher;
//    }
//
//    /**
//     * 自定义Realm
//     * @return
//     */
//    @Bean
//    public UserRealm userRealm(){
//        UserRealm userRealm = new UserRealm();
//        userRealm.setCredentialsMatcher(hashedCredentialsMatcher());
//        return userRealm;
//    }
//
//    /**
//     * 安全器管理
//     * @return
//     */
//    @Bean
//    public DefaultWebSecurityManager securityManager(){
//        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setRealm(userRealm());
//        return securityManager;
//    }
//
//
//    @Bean(name = "shiroFilter")
//    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
//        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
//        shiroFilterFactoryBean.setSecurityManager(securityManager);
//        return shiroFilterFactoryBean;
//    }
//
////    /**
////     * 设置过滤规则
////     * @param securityManager
////     * @return
////     */
////    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager){
////        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
////        shiroFilterFactoryBean.setSecurityManager(securityManager);
////        shiroFilterFactoryBean.setLoginUrl("/test_shiro/login");
////        shiroFilterFactoryBean.setSuccessUrl("/test_shiro/index");
////        shiroFilterFactoryBean.setUnauthorizedUrl("/test_shiro/unauth");
////
////        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
////        filterChainDefinitionMap.put("/static/**", "anon");
////        filterChainDefinitionMap.put("/login", "anon");
////        filterChainDefinitionMap.put("/captcha.jpg", "anon");
////        filterChainDefinitionMap.put("/favicon.ico", "anon");
////        filterChainDefinitionMap.put("/**", "authc");
////
////        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
////        return shiroFilterFactoryBean;
////    }
}
